/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package glamourbeautyclinic.customer;

import java.awt.Dimension;
import java.awt.Toolkit;
import images.GlamourImage;
import java.awt.Color;
import java.awt.Component;
import static java.awt.Component.CENTER_ALIGNMENT;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowSorter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import sun.awt.image.ToolkitImage;
import util.DBConnection;

/**
 *
 * @author anh
 */
public class FrmCustomer extends javax.swing.JInternalFrame {
    
    GlamourImage GI = new GlamourImage();
    DBConnection dc;
    
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    Calendar cal = Calendar.getInstance();
    String User, custPicName = null;
    Image custImg = null;
    boolean gender, tblCustEventOn = true, lostFocusEventOn = true;
    enum mode { Insert, Update}
    mode type;
    enum searchBy { byID, byName}
    searchBy way = searchBy.byID;
    ArrayList param = new ArrayList();
    
    DefaultTableModel custModel, appModel;
    Vector transpoter = new Vector();
    
    /**
     * Creates new form frmSearchCustomer
     */
    public FrmCustomer(DBConnection dc) {
        
        initComponents();
        
        //------------------Additional coding-------------------
        GI.addIconFor(this);
        this.dc = dc;
        
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        // Can be resize height parameter to proper scale
        this.setLocation((screenSize.width - this.getWidth()) / 2, 10);
        
        custModel = (DefaultTableModel)tblCust.getModel();
        RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(custModel);
        tblCust.setRowSorter(sorter);
        tblCust.setComponentPopupMenu(pmnCust);
        tblCust.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent evt) {
                if (tblCustEventOn)
                    tblCustValueChanged(evt);
            }
        });
        
        appModel = (DefaultTableModel)tblApp.getModel();
        sorter = new TableRowSorter<TableModel>(appModel);
        tblApp.setRowSorter(sorter);
        tblApp.setComponentPopupMenu(pmnApp);
        tblApp.setDefaultRenderer(Object.class, new tblAppRender());
        tblApp.setDefaultRenderer(Boolean.class, new tblAppRender());
    }
    
    private void getAuthority() {
        if (User == null) {
            try {
                User = (String)getDesktopPane().getTopLevelAncestor().getClass().getDeclaredField("user").get(getDesktopPane().getTopLevelAncestor());
            } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
                Logger.getLogger(FrmCustomer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grpGender = new javax.swing.ButtonGroup();
        grpFilter = new javax.swing.ButtonGroup();
        pmnCust = new javax.swing.JPopupMenu();
        mniNewApp = new javax.swing.JMenuItem();
        mniNewAppFull = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mniViewApp = new javax.swing.JMenuItem();
        pmnApp = new javax.swing.JPopupMenu();
        mniSelectAll = new javax.swing.JMenuItem();
        mniDeselect = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        mniDelete = new javax.swing.JMenuItem();
        mniPreBill = new javax.swing.JMenuItem();
        pnlCust = new javax.swing.JPanel();
        btnInsert = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        pnlChildCust = new javax.swing.JPanel();
        lblCustID = new javax.swing.JLabel();
        lblRequired1 = new javax.swing.JLabel();
        txtCustID = new javax.swing.JTextField();
        lblCustName = new javax.swing.JLabel();
        lblRequired2 = new javax.swing.JLabel();
        txtCustName = new javax.swing.JTextField();
        lblAdd = new javax.swing.JLabel();
        txtAdd = new javax.swing.JTextField();
        lblDOB = new javax.swing.JLabel();
        calDOB = new org.freixas.jcalendar.JCalendarCombo();
        lblGender = new javax.swing.JLabel();
        radMale = new javax.swing.JRadioButton();
        radFemale = new javax.swing.JRadioButton();
        lblEmail = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        lblPic = new PhotoFrame();
        btnAddPic = new javax.swing.JButton();
        lblCustMsg = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        tabCust = new javax.swing.JTabbedPane();
        pnlSearch = new javax.swing.JPanel();
        radCustID = new javax.swing.JRadioButton();
        radCustName = new javax.swing.JRadioButton();
        txtCustIDFilter = new javax.swing.JTextField();
        txtCustNameFilter = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        btnFindAll = new javax.swing.JButton();
        pnlSearchChild = new javax.swing.JPanel();
        jspCust = new javax.swing.JScrollPane();
        tblCust = new javax.swing.JTable();
        lblCustFound = new javax.swing.JLabel();
        pnlApp = new javax.swing.JPanel();
        btnSelectAll = new javax.swing.JButton();
        btnDeselect = new javax.swing.JButton();
        btnPreBill = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        pnlAppChild = new javax.swing.JPanel();
        jspCust1 = new javax.swing.JScrollPane();
        tblApp = new javax.swing.JTable();
        lblAppFound = new javax.swing.JLabel();
        lblCustID_App = new javax.swing.JLabel();
        txtCustID_App = new javax.swing.JTextField();
        btnGetApp = new javax.swing.JButton();
        pnlButton = new javax.swing.JPanel();
        btnExit = new javax.swing.JButton();

        mniNewApp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Insert.png"))); // NOI18N
        mniNewApp.setText("New appointment");
        mniNewApp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniNewAppActionPerformed(evt);
            }
        });
        pmnCust.add(mniNewApp);

        mniNewAppFull.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Insert.png"))); // NOI18N
        mniNewAppFull.setText("New appointment (in details)");
        mniNewAppFull.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniNewAppFullActionPerformed(evt);
            }
        });
        pmnCust.add(mniNewAppFull);
        pmnCust.add(jSeparator1);

        mniViewApp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Search.png"))); // NOI18N
        mniViewApp.setText("View all appointment");
        mniViewApp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniViewAppActionPerformed(evt);
            }
        });
        pmnCust.add(mniViewApp);

        mniSelectAll.setText("Select all");
        mniSelectAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniSelectAllActionPerformed(evt);
            }
        });
        pmnApp.add(mniSelectAll);

        mniDeselect.setText("Deselect");
        mniDeselect.setEnabled(false);
        mniDeselect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniDeselectActionPerformed(evt);
            }
        });
        pmnApp.add(mniDeselect);
        pmnApp.add(jSeparator2);

        mniDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Delete.png"))); // NOI18N
        mniDelete.setText("Delete");
        mniDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniDeleteActionPerformed(evt);
            }
        });
        pmnApp.add(mniDelete);

        mniPreBill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/bill.png"))); // NOI18N
        mniPreBill.setText("Pre-bill");
        mniPreBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniPreBillActionPerformed(evt);
            }
        });
        pmnApp.add(mniPreBill);

        setClosable(true);
        setIconifiable(true);
        setTitle("Customer management");
        setMaximumSize(new java.awt.Dimension(672, 634));

        pnlCust.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.highlight"));
        pnlCust.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Customer information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12), new java.awt.Color(51, 51, 51))); // NOI18N
        pnlCust.setOpaque(false);

        btnInsert.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnInsert.setForeground(new java.awt.Color(0, 51, 51));
        btnInsert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Insert.png"))); // NOI18N
        btnInsert.setMnemonic('x');
        btnInsert.setText("Insert");
        btnInsert.setToolTipText("Save to database");
        btnInsert.setBorder(null);
        btnInsert.setContentAreaFilled(false);
        btnInsert.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnInsert.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnInsert.setName(""); // NOI18N
        btnInsert.setOpaque(true);
        btnInsert.setPreferredSize(new java.awt.Dimension(59, 22));
        btnInsert.setRolloverEnabled(false);
        btnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsertActionPerformed(evt);
            }
        });

        btnUpdate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnUpdate.setForeground(new java.awt.Color(0, 51, 51));
        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Update.png"))); // NOI18N
        btnUpdate.setMnemonic('x');
        btnUpdate.setText("Update");
        btnUpdate.setToolTipText("Save to database");
        btnUpdate.setBorder(null);
        btnUpdate.setContentAreaFilled(false);
        btnUpdate.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnUpdate.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnUpdate.setName(""); // NOI18N
        btnUpdate.setOpaque(true);
        btnUpdate.setPreferredSize(new java.awt.Dimension(59, 22));
        btnUpdate.setRolloverEnabled(false);
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        pnlChildCust.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102)));

        lblCustID.setDisplayedMnemonic('D');
        lblCustID.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblCustID.setForeground(new java.awt.Color(0, 51, 255));
        lblCustID.setText("Customer ID");
        lblCustID.setPreferredSize(new java.awt.Dimension(38, 20));

        lblRequired1.setForeground(new java.awt.Color(255, 0, 0));
        lblRequired1.setText("(*)");
        lblRequired1.setPreferredSize(new java.awt.Dimension(38, 20));

        txtCustID.setForeground(new java.awt.Color(0, 102, 51));
        txtCustID.setEnabled(false);
        txtCustID.setPreferredSize(new java.awt.Dimension(56, 20));
        txtCustID.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCustIDFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCustIDFocusLost(evt);
            }
        });

        lblCustName.setDisplayedMnemonic('n');
        lblCustName.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblCustName.setForeground(new java.awt.Color(0, 51, 255));
        lblCustName.setText("Customer name");
        lblCustName.setPreferredSize(new java.awt.Dimension(38, 20));

        lblRequired2.setForeground(new java.awt.Color(255, 0, 0));
        lblRequired2.setText("(*)");
        lblRequired2.setPreferredSize(new java.awt.Dimension(38, 20));

        txtCustName.setForeground(new java.awt.Color(0, 102, 51));
        txtCustName.setEnabled(false);
        txtCustName.setPreferredSize(new java.awt.Dimension(56, 20));
        txtCustName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCustNameFocusGained(evt);
            }
        });

        lblAdd.setDisplayedMnemonic('t');
        lblAdd.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblAdd.setForeground(new java.awt.Color(0, 51, 255));
        lblAdd.setText("Address");
        lblAdd.setPreferredSize(new java.awt.Dimension(38, 20));

        txtAdd.setForeground(new java.awt.Color(0, 102, 51));
        txtAdd.setEnabled(false);
        txtAdd.setPreferredSize(new java.awt.Dimension(56, 20));
        txtAdd.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtAddFocusGained(evt);
            }
        });

        lblDOB.setDisplayedMnemonic('W');
        lblDOB.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblDOB.setForeground(new java.awt.Color(0, 51, 255));
        lblDOB.setText("Date of birth");
        lblDOB.setPreferredSize(new java.awt.Dimension(38, 20));

        calDOB.setForeground(new java.awt.Color(0, 102, 51));
        calDOB.setEnabled(false);
        calDOB.setPreferredSize(new java.awt.Dimension(56, 20));

        lblGender.setDisplayedMnemonic('t');
        lblGender.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblGender.setForeground(new java.awt.Color(0, 51, 255));
        lblGender.setText("Gender");
        lblGender.setPreferredSize(new java.awt.Dimension(38, 20));

        radMale.setBackground(new java.awt.Color(255, 255, 255));
        grpGender.add(radMale);
        radMale.setForeground(new java.awt.Color(0, 102, 51));
        radMale.setSelected(true);
        radMale.setText("Male");
        radMale.setBorder(null);
        radMale.setBorderPainted(true);
        radMale.setEnabled(false);
        radMale.setFocusPainted(false);
        radMale.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        radMale.setOpaque(false);
        radMale.setPreferredSize(new java.awt.Dimension(43, 20));
        radMale.setRolloverEnabled(false);

        radFemale.setBackground(new java.awt.Color(255, 255, 255));
        grpGender.add(radFemale);
        radFemale.setForeground(new java.awt.Color(0, 102, 51));
        radFemale.setText("Female");
        radFemale.setBorder(null);
        radFemale.setBorderPainted(true);
        radFemale.setEnabled(false);
        radFemale.setFocusPainted(false);
        radFemale.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        radFemale.setOpaque(false);
        radFemale.setPreferredSize(new java.awt.Dimension(43, 20));
        radFemale.setRolloverEnabled(false);

        lblEmail.setDisplayedMnemonic('t');
        lblEmail.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblEmail.setForeground(new java.awt.Color(0, 51, 255));
        lblEmail.setText("Email");
        lblEmail.setPreferredSize(new java.awt.Dimension(38, 20));

        txtEmail.setForeground(new java.awt.Color(0, 102, 51));
        txtEmail.setEnabled(false);
        txtEmail.setPreferredSize(new java.awt.Dimension(56, 20));
        txtEmail.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtEmailFocusGained(evt);
            }
        });

        lblPic.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblPic.setDoubleBuffered(true);

        btnAddPic.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnAddPic.setForeground(new java.awt.Color(0, 51, 51));
        btnAddPic.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Insert.png"))); // NOI18N
        btnAddPic.setMnemonic('x');
        btnAddPic.setText("Add picture");
        btnAddPic.setToolTipText("Save to database");
        btnAddPic.setBorder(null);
        btnAddPic.setContentAreaFilled(false);
        btnAddPic.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnAddPic.setEnabled(false);
        btnAddPic.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnAddPic.setName(""); // NOI18N
        btnAddPic.setOpaque(true);
        btnAddPic.setPreferredSize(new java.awt.Dimension(59, 22));
        btnAddPic.setRolloverEnabled(false);
        btnAddPic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddPicActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlChildCustLayout = new javax.swing.GroupLayout(pnlChildCust);
        pnlChildCust.setLayout(pnlChildCustLayout);
        pnlChildCustLayout.setHorizontalGroup(
            pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlChildCustLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlChildCustLayout.createSequentialGroup()
                        .addComponent(lblCustID, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblRequired1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlChildCustLayout.createSequentialGroup()
                        .addGroup(pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblGender, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDOB, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblCustName, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblRequired2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCustID, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlChildCustLayout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addGroup(pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCustName, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(calDOB, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(pnlChildCustLayout.createSequentialGroup()
                                .addComponent(radMale, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(radFemale, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAddPic, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblPic, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pnlChildCustLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {lblAdd, lblCustID, lblCustName, lblDOB, lblEmail, lblGender});

        pnlChildCustLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {lblRequired1, lblRequired2});

        pnlChildCustLayout.setVerticalGroup(
            pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlChildCustLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlChildCustLayout.createSequentialGroup()
                        .addGroup(pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlChildCustLayout.createSequentialGroup()
                                .addComponent(txtCustID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCustName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                                .addComponent(calDOB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(radMale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(radFemale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(lblPic, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnAddPic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(pnlChildCustLayout.createSequentialGroup()
                        .addGroup(pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblCustID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblRequired1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlChildCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCustName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblRequired2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblDOB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        lblCustMsg.setForeground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        lblCustMsg.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCustMsg.setText("Invalid Customer ID. Mask: Cust000001.");
        lblCustMsg.setPreferredSize(new java.awt.Dimension(38, 20));

        btnSave.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnSave.setForeground(new java.awt.Color(0, 51, 51));
        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Save.png"))); // NOI18N
        btnSave.setMnemonic('x');
        btnSave.setText("Save");
        btnSave.setToolTipText("Save to database");
        btnSave.setBorder(null);
        btnSave.setContentAreaFilled(false);
        btnSave.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnSave.setEnabled(false);
        btnSave.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnSave.setName(""); // NOI18N
        btnSave.setOpaque(true);
        btnSave.setPreferredSize(new java.awt.Dimension(59, 22));
        btnSave.setRolloverEnabled(false);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnCancel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnCancel.setForeground(new java.awt.Color(0, 51, 51));
        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Cancel.png"))); // NOI18N
        btnCancel.setMnemonic('x');
        btnCancel.setText("Cancel");
        btnCancel.setToolTipText("Save to database");
        btnCancel.setBorder(null);
        btnCancel.setContentAreaFilled(false);
        btnCancel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnCancel.setEnabled(false);
        btnCancel.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnCancel.setName(""); // NOI18N
        btnCancel.setOpaque(true);
        btnCancel.setPreferredSize(new java.awt.Dimension(59, 22));
        btnCancel.setRolloverEnabled(false);
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlCustLayout = new javax.swing.GroupLayout(pnlCust);
        pnlCust.setLayout(pnlCustLayout);
        pnlCustLayout.setHorizontalGroup(
            pnlCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCustLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlChildCust, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnlCustLayout.createSequentialGroup()
                        .addGroup(pnlCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlCustLayout.createSequentialGroup()
                                .addComponent(btnInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnlCustLayout.createSequentialGroup()
                                .addComponent(lblCustMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pnlCustLayout.setVerticalGroup(
            pnlCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCustLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlChildCust, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlCustLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCustMsg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        tabCust.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabCustStateChanged(evt);
            }
        });

        pnlSearch.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        radCustID.setBackground(new java.awt.Color(255, 255, 255));
        grpFilter.add(radCustID);
        radCustID.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        radCustID.setForeground(new java.awt.Color(0, 51, 255));
        radCustID.setSelected(true);
        radCustID.setText("Customer ID");
        radCustID.setBorder(null);
        radCustID.setBorderPainted(true);
        radCustID.setFocusPainted(false);
        radCustID.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        radCustID.setOpaque(false);
        radCustID.setPreferredSize(new java.awt.Dimension(43, 20));
        radCustID.setRolloverEnabled(false);
        radCustID.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                radCustIDItemStateChanged(evt);
            }
        });

        radCustName.setBackground(new java.awt.Color(255, 255, 255));
        grpFilter.add(radCustName);
        radCustName.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        radCustName.setForeground(new java.awt.Color(0, 51, 255));
        radCustName.setText("Customer name");
        radCustName.setBorder(null);
        radCustName.setBorderPainted(true);
        radCustName.setFocusPainted(false);
        radCustName.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        radCustName.setOpaque(false);
        radCustName.setPreferredSize(new java.awt.Dimension(43, 20));
        radCustName.setRolloverEnabled(false);

        txtCustIDFilter.setForeground(new java.awt.Color(0, 102, 51));
        txtCustIDFilter.setPreferredSize(new java.awt.Dimension(56, 20));
        txtCustIDFilter.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCustIDFilterFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCustIDFilterFocusLost(evt);
            }
        });

        txtCustNameFilter.setForeground(new java.awt.Color(0, 102, 51));
        txtCustNameFilter.setEnabled(false);
        txtCustNameFilter.setPreferredSize(new java.awt.Dimension(56, 20));
        txtCustNameFilter.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCustNameFilterFocusGained(evt);
            }
        });

        btnSearch.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnSearch.setForeground(new java.awt.Color(0, 51, 51));
        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Search.png"))); // NOI18N
        btnSearch.setMnemonic('S');
        btnSearch.setText("Search");
        btnSearch.setToolTipText("Find customer");
        btnSearch.setBorder(null);
        btnSearch.setContentAreaFilled(false);
        btnSearch.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnSearch.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnSearch.setName(""); // NOI18N
        btnSearch.setOpaque(true);
        btnSearch.setPreferredSize(new java.awt.Dimension(59, 22));
        btnSearch.setRolloverEnabled(false);
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        btnFindAll.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnFindAll.setForeground(new java.awt.Color(0, 51, 51));
        btnFindAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/SelectAll.png"))); // NOI18N
        btnFindAll.setMnemonic('S');
        btnFindAll.setText("Find All");
        btnFindAll.setToolTipText("Find customer");
        btnFindAll.setBorder(null);
        btnFindAll.setContentAreaFilled(false);
        btnFindAll.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnFindAll.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnFindAll.setName(""); // NOI18N
        btnFindAll.setOpaque(true);
        btnFindAll.setPreferredSize(new java.awt.Dimension(59, 22));
        btnFindAll.setRolloverEnabled(false);
        btnFindAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFindAllActionPerformed(evt);
            }
        });

        pnlSearchChild.setBackground(new java.awt.Color(255, 255, 255));
        pnlSearchChild.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        tblCust.setForeground(new java.awt.Color(0, 102, 51));
        tblCust.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Full name", "Female", "Address", "Date of birth"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblCust.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblCust.getTableHeader().setReorderingAllowed(false);
        tblCust.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCustMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblCustMousePressed(evt);
            }
        });
        jspCust.setViewportView(tblCust);
        if (tblCust.getColumnModel().getColumnCount() > 0) {
            tblCust.getColumnModel().getColumn(0).setMinWidth(0);
            tblCust.getColumnModel().getColumn(0).setPreferredWidth(0);
            tblCust.getColumnModel().getColumn(0).setMaxWidth(0);
            tblCust.getColumnModel().getColumn(1).setMinWidth(100);
            tblCust.getColumnModel().getColumn(1).setPreferredWidth(100);
            tblCust.getColumnModel().getColumn(1).setMaxWidth(100);
            tblCust.getColumnModel().getColumn(2).setMinWidth(60);
            tblCust.getColumnModel().getColumn(2).setPreferredWidth(60);
            tblCust.getColumnModel().getColumn(2).setMaxWidth(60);
            tblCust.getColumnModel().getColumn(4).setMinWidth(100);
            tblCust.getColumnModel().getColumn(4).setPreferredWidth(100);
            tblCust.getColumnModel().getColumn(4).setMaxWidth(100);
        }

        javax.swing.GroupLayout pnlSearchChildLayout = new javax.swing.GroupLayout(pnlSearchChild);
        pnlSearchChild.setLayout(pnlSearchChildLayout);
        pnlSearchChildLayout.setHorizontalGroup(
            pnlSearchChildLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jspCust, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        pnlSearchChildLayout.setVerticalGroup(
            pnlSearchChildLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jspCust, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        lblCustFound.setForeground(new java.awt.Color(240, 240, 240));
        lblCustFound.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblCustFound.setText("0 records found");
        lblCustFound.setPreferredSize(new java.awt.Dimension(38, 20));

        javax.swing.GroupLayout pnlSearchLayout = new javax.swing.GroupLayout(pnlSearch);
        pnlSearch.setLayout(pnlSearchLayout);
        pnlSearchLayout.setHorizontalGroup(
            pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSearchLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlSearchLayout.createSequentialGroup()
                        .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(radCustID, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(radCustName, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(41, 41, 41)
                        .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCustIDFilter, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCustNameFilter, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(39, 39, 39)
                        .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnFindAll, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 98, Short.MAX_VALUE))
                    .addComponent(pnlSearchChild, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblCustFound, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pnlSearchLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {radCustID, radCustName});

        pnlSearchLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtCustIDFilter, txtCustNameFilter});

        pnlSearchLayout.setVerticalGroup(
            pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSearchLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCustIDFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(radCustID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radCustName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCustNameFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnFindAll, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlSearchChild, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCustFound, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabCust.addTab("Search", pnlSearch);

        pnlApp.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        btnSelectAll.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnSelectAll.setForeground(new java.awt.Color(0, 51, 51));
        btnSelectAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/SelectAll.png"))); // NOI18N
        btnSelectAll.setMnemonic('a');
        btnSelectAll.setText("Select all");
        btnSelectAll.setToolTipText("Check all select combo box");
        btnSelectAll.setBorder(null);
        btnSelectAll.setContentAreaFilled(false);
        btnSelectAll.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnSelectAll.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnSelectAll.setName(""); // NOI18N
        btnSelectAll.setOpaque(true);
        btnSelectAll.setPreferredSize(new java.awt.Dimension(59, 22));
        btnSelectAll.setRolloverEnabled(false);
        btnSelectAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectAllActionPerformed(evt);
            }
        });

        btnDeselect.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnDeselect.setForeground(new java.awt.Color(0, 51, 51));
        btnDeselect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Deselect.png"))); // NOI18N
        btnDeselect.setMnemonic('e');
        btnDeselect.setText("Deselect");
        btnDeselect.setToolTipText("Uncheck all select combo box");
        btnDeselect.setBorder(null);
        btnDeselect.setContentAreaFilled(false);
        btnDeselect.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnDeselect.setEnabled(false);
        btnDeselect.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnDeselect.setName(""); // NOI18N
        btnDeselect.setOpaque(true);
        btnDeselect.setPreferredSize(new java.awt.Dimension(59, 22));
        btnDeselect.setRolloverEnabled(false);
        btnDeselect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeselectActionPerformed(evt);
            }
        });

        btnPreBill.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnPreBill.setForeground(new java.awt.Color(0, 51, 51));
        btnPreBill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/bill.png"))); // NOI18N
        btnPreBill.setMnemonic('e');
        btnPreBill.setText("Pre-bill");
        btnPreBill.setToolTipText("Uncheck all select combo box");
        btnPreBill.setBorder(null);
        btnPreBill.setContentAreaFilled(false);
        btnPreBill.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnPreBill.setEnabled(false);
        btnPreBill.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnPreBill.setName(""); // NOI18N
        btnPreBill.setOpaque(true);
        btnPreBill.setPreferredSize(new java.awt.Dimension(59, 22));
        btnPreBill.setRolloverEnabled(false);
        btnPreBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreBillActionPerformed(evt);
            }
        });

        btnDelete.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnDelete.setForeground(new java.awt.Color(0, 51, 51));
        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Delete.png"))); // NOI18N
        btnDelete.setMnemonic('e');
        btnDelete.setText("Delete");
        btnDelete.setToolTipText("Uncheck all select combo box");
        btnDelete.setBorder(null);
        btnDelete.setContentAreaFilled(false);
        btnDelete.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnDelete.setEnabled(false);
        btnDelete.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnDelete.setName(""); // NOI18N
        btnDelete.setOpaque(true);
        btnDelete.setPreferredSize(new java.awt.Dimension(59, 22));
        btnDelete.setRolloverEnabled(false);
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        pnlAppChild.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        tblApp.setForeground(new java.awt.Color(0, 102, 51));
        tblApp.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Select", "Date", "Customer name", "Status", "Error"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblApp.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblApp.getTableHeader().setReorderingAllowed(false);
        tblApp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAppMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblAppMousePressed(evt);
            }
        });
        jspCust1.setViewportView(tblApp);
        if (tblApp.getColumnModel().getColumnCount() > 0) {
            tblApp.getColumnModel().getColumn(0).setMinWidth(0);
            tblApp.getColumnModel().getColumn(0).setPreferredWidth(0);
            tblApp.getColumnModel().getColumn(0).setMaxWidth(0);
            tblApp.getColumnModel().getColumn(1).setMinWidth(50);
            tblApp.getColumnModel().getColumn(1).setPreferredWidth(50);
            tblApp.getColumnModel().getColumn(1).setMaxWidth(50);
            tblApp.getColumnModel().getColumn(2).setMinWidth(80);
            tblApp.getColumnModel().getColumn(2).setPreferredWidth(80);
            tblApp.getColumnModel().getColumn(2).setMaxWidth(80);
            tblApp.getColumnModel().getColumn(3).setMinWidth(100);
            tblApp.getColumnModel().getColumn(3).setPreferredWidth(100);
            tblApp.getColumnModel().getColumn(3).setMaxWidth(100);
            tblApp.getColumnModel().getColumn(4).setMinWidth(70);
            tblApp.getColumnModel().getColumn(4).setPreferredWidth(70);
            tblApp.getColumnModel().getColumn(4).setMaxWidth(70);
        }

        javax.swing.GroupLayout pnlAppChildLayout = new javax.swing.GroupLayout(pnlAppChild);
        pnlAppChild.setLayout(pnlAppChildLayout);
        pnlAppChildLayout.setHorizontalGroup(
            pnlAppChildLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jspCust1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
        );
        pnlAppChildLayout.setVerticalGroup(
            pnlAppChildLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jspCust1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        lblAppFound.setForeground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        lblAppFound.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblAppFound.setText("0 records found");
        lblAppFound.setPreferredSize(new java.awt.Dimension(38, 20));

        lblCustID_App.setDisplayedMnemonic('D');
        lblCustID_App.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblCustID_App.setForeground(new java.awt.Color(0, 51, 255));
        lblCustID_App.setText("Customer ID");
        lblCustID_App.setPreferredSize(new java.awt.Dimension(38, 20));

        txtCustID_App.setForeground(new java.awt.Color(0, 102, 51));
        txtCustID_App.setPreferredSize(new java.awt.Dimension(56, 20));
        txtCustID_App.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCustID_AppFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCustID_AppFocusLost(evt);
            }
        });

        btnGetApp.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnGetApp.setForeground(new java.awt.Color(0, 51, 51));
        btnGetApp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/filter.png"))); // NOI18N
        btnGetApp.setMnemonic('S');
        btnGetApp.setText("Get appointment");
        btnGetApp.setToolTipText("Find customer");
        btnGetApp.setBorder(null);
        btnGetApp.setContentAreaFilled(false);
        btnGetApp.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnGetApp.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnGetApp.setName(""); // NOI18N
        btnGetApp.setOpaque(true);
        btnGetApp.setPreferredSize(new java.awt.Dimension(100, 22));
        btnGetApp.setRolloverEnabled(false);
        btnGetApp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGetAppActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlAppLayout = new javax.swing.GroupLayout(pnlApp);
        pnlApp.setLayout(pnlAppLayout);
        pnlAppLayout.setHorizontalGroup(
            pnlAppLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAppLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlAppLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlAppChild, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblAppFound, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnlAppLayout.createSequentialGroup()
                        .addComponent(btnSelectAll, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnDeselect, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnPreBill, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlAppLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(lblCustID_App, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCustID_App, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnGetApp, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(70, 70, 70)))
                .addContainerGap())
        );
        pnlAppLayout.setVerticalGroup(
            pnlAppLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAppLayout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addGroup(pnlAppLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCustID_App, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCustID_App, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGetApp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlAppLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSelectAll, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDeselect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPreBill, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlAppChild, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAppFound, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabCust.addTab("Appointment", pnlApp);

        pnlButton.setOpaque(false);

        btnExit.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnExit.setForeground(new java.awt.Color(0, 51, 51));
        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/quit.png"))); // NOI18N
        btnExit.setMnemonic('x');
        btnExit.setText("Exit");
        btnExit.setToolTipText("Close window");
        btnExit.setBorder(null);
        btnExit.setContentAreaFilled(false);
        btnExit.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnExit.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnExit.setName(""); // NOI18N
        btnExit.setOpaque(true);
        btnExit.setPreferredSize(new java.awt.Dimension(59, 22));
        btnExit.setRolloverEnabled(false);
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });
        pnlButton.add(btnExit);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlCust, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlButton, javax.swing.GroupLayout.PREFERRED_SIZE, 458, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(tabCust))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlCust, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(tabCust, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed
        clearForm();
        type = mode.Insert;
        tblCustEventOn = false;
        lostFocusEventOn = false;
        txtCustID.setText(generateCustID());
        txtCustName.grabFocus();
        setEnabledForm(true,true);
        setBackColorForm(new Color(255,255,255),true);
        
        // Toggle on Insert button
        lblCustMsg.setForeground(new Color(240,240,240));
        btnUpdate.setEnabled(false);
        btnInsert.setForeground(new Color(255,0,0));
        
        //Clear picture
        custImg = null;
        custPicName = null;
        lblPic.repaintBackground(custImg);
    
    }//GEN-LAST:event_btnInsertActionPerformed
    
    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        type = mode.Update;
        tblCustEventOn = false;
        if (txtCustID.getText().isEmpty()){
            txtCustID.grabFocus();
            setEnabledForm(true,false);
            setBackColorForm(new Color(255,255,255),false);
        } else {
            txtCustName.grabFocus();
            setEnabledForm(true,true);
            setBackColorForm(new Color(255,255,255),true);
        }
        
        // Toggle on Update button
        lblCustMsg.setForeground(new Color(240,240,240));
        btnInsert.setEnabled(false);
        btnUpdate.setForeground(new Color(255,0,0));
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnAddPicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddPicActionPerformed
        addPicture();
    }//GEN-LAST:event_btnAddPicActionPerformed
    
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if (isValidated()){
            getAuthority();
            saveData();
            updateTable();
            setEnabledForm(false,false);
            setBackColorForm(new Color(240,240,240),false);
            clearForm();
            resetEnvir();
        }
    }
    
    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        lostFocusEventOn = false;
        setEnabledForm(false,false);
        setBackColorForm(new Color(240,240,240),false);
        clearForm();
        resetEnvir();
        lblCustMsg.setForeground(new Color(240,240,240));
    }//GEN-LAST:event_btnCancelActionPerformed

    private void txtCustIDFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCustIDFocusGained
        selectText("txtCustID");
    }//GEN-LAST:event_txtCustIDFocusGained

    private void txtCustIDFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCustIDFocusLost
        if (lostFocusEventOn) {
            if(autocompletedPattern())  
                bindingData(txtCustID.getText());
        } else
            txtCustID.setBackground(new Color(240,240,240));
    }//GEN-LAST:event_txtCustIDFocusLost

    private void txtCustNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCustNameFocusGained
        selectText("txtCustName");
    }//GEN-LAST:event_txtCustNameFocusGained

    private void txtAddFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtAddFocusGained
        selectText("txtAdd");
    }//GEN-LAST:event_txtAddFocusGained

    private void txtEmailFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtEmailFocusGained
        selectText("txtEmail");
    }//GEN-LAST:event_txtEmailFocusGained

    private void radCustIDItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_radCustIDItemStateChanged
        if(radCustID.isSelected()) {
            lostFocusEventOn = true;
            way = searchBy.byID;
            txtCustIDFilter.setEnabled(true);
            txtCustIDFilter.grabFocus();
            txtCustNameFilter.setEnabled(false);
            txtCustNameFilter.setText("");
        } else {
            lostFocusEventOn = false;
            way = searchBy.byName;
            txtCustIDFilter.setEnabled(false);
            txtCustNameFilter.setEnabled(true);
            txtCustNameFilter.grabFocus();
            txtCustIDFilter.setText("");
        }
    }//GEN-LAST:event_radCustIDItemStateChanged

    private void txtCustIDFilterFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCustIDFilterFocusGained
        selectText("txtCustIDFilter");
    }//GEN-LAST:event_txtCustIDFilterFocusGained

    private void txtCustIDFilterFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCustIDFilterFocusLost
        
        if (lostFocusEventOn) {
            boolean mat_Short = Pattern.matches("\\d{1,6}", txtCustIDFilter.getText());
            int length = txtCustIDFilter.getText().length();
        
            if (mat_Short) 
                txtCustIDFilter.setText("Cust"+"000000".substring(0, 6-length)+txtCustIDFilter.getText());
            else
                txtCustIDFilter.grabFocus();
        }
    }//GEN-LAST:event_txtCustIDFilterFocusLost

    private void txtCustNameFilterFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCustNameFilterFocusGained
        selectText("txtCustNameFilter");
    }//GEN-LAST:event_txtCustNameFilterFocusGained

    private void tblCustMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCustMouseClicked
        //Binding data
        int rowSelected = tblCust.getSelectedRow();
        if (rowSelected != -1){
            bindingData((String)tblCust.getValueAt(rowSelected, 0));
        }

        // Active UPDATE mode
        if (evt.getClickCount() == 2 && !evt.isConsumed()) {
            evt.consume();
            btnUpdateActionPerformed(null);
        }    
    }//GEN-LAST:event_tblCustMouseClicked
    
private void tblCustMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCustMousePressed
        //Select active row
        Point point = evt.getPoint();
        int currentRow = tblCust.rowAtPoint(point);
        tblCust.setRowSelectionInterval(currentRow, currentRow);
    }//GEN-LAST:event_tblCustMousePressed

    private void tblCustValueChanged(ListSelectionEvent evt) {
        //Binding data
        int rowSelected = tblCust.getSelectedRow();
        if (rowSelected != -1){
            bindingData((String)tblCust.getValueAt(rowSelected, 0));
        }
    }
    
    private void mniNewAppActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniNewAppActionPerformed
        String custID  = (String)tblCust.getValueAt(tblCust.getSelectedRow(), 0);
        if (JOptionPane.showConfirmDialog(this, "Make an appoitment for '"+custID+"' ?", "Make appointment confirm", JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_OPTION)
            newApp(custID);
    }//GEN-LAST:event_mniNewAppActionPerformed

    
    private void mniNewAppFullActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniNewAppFullActionPerformed
        mniNewAppActionPerformed(null);
        displayAppForm();
    }//GEN-LAST:event_mniNewAppFullActionPerformed

    private void mniViewAppActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniViewAppActionPerformed
        String custID  = (String)tblCust.getValueAt(tblCust.getSelectedRow(), 0);
        tabCust.setSelectedIndex(1);
        txtCustID_App.setText(custID);
        getApp(custID);
    }//GEN-LAST:event_mniViewAppActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        if (radCustID.isSelected()) {
            if (searchCust(false,txtCustIDFilter.getText())==0)
                selectText("txtCustIDFilter");
        } else {
            if(searchCust(false,"%" + txtCustNameFilter.getText() + "%")==0)
                selectText("txtCustNameFilter");
        }
    }//GEN-LAST:event_btnSearchActionPerformed

    private void btnFindAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFindAllActionPerformed
        searchCust(true,null);
    }//GEN-LAST:event_btnFindAllActionPerformed

    private void txtCustID_AppFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCustID_AppFocusGained
        selectText("txtCustID_App");
    }//GEN-LAST:event_txtCustID_AppFocusGained

    private void txtCustID_AppFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCustID_AppFocusLost
        
        boolean mat_Short = Pattern.matches("\\d{1,6}", txtCustID_App.getText());
        int length = txtCustID_App.getText().length();
        
        if (mat_Short) 
            txtCustID_App.setText("Cust"+"000000".substring(0, 6-length)+txtCustID_App.getText());
        else
            txtCustID_App.grabFocus();        
    }//GEN-LAST:event_txtCustID_AppFocusLost

    private void tblAppMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAppMouseClicked
        
        int noRow = appModel.getRowCount();
        int noCount = 0;
        for (int i = 0; i < noRow; i++) {
            if ((boolean)appModel.getValueAt(i, 1) == true)
                noCount++;
        }
        
        if (noCount == noRow) {
            btnSelectAll.setEnabled(false);
            btnDeselect.setEnabled(true);
            mniSelectAll.setEnabled(false);
            mniDeselect.setEnabled(true);
            btnPreBill.setEnabled(true);
            btnDelete.setEnabled(true);
            mniPreBill.setEnabled(true);
            mniDelete.setEnabled(true);
        } else if (noCount == 0) {
            btnSelectAll.setEnabled(true);
            btnDeselect.setEnabled(false);
            mniSelectAll.setEnabled(true);
            mniDeselect.setEnabled(false);
            btnPreBill.setEnabled(false);
            btnDelete.setEnabled(false);
            mniPreBill.setEnabled(false);
            mniDelete.setEnabled(false);
        } else {
            btnSelectAll.setEnabled(true);
            btnDeselect.setEnabled(true);
            mniSelectAll.setEnabled(true);
            mniDeselect.setEnabled(true);
            btnPreBill.setEnabled(true);
            btnDelete.setEnabled(true);
            mniPreBill.setEnabled(true);
            mniDelete.setEnabled(true);
        }
    }//GEN-LAST:event_tblAppMouseClicked

    private void tblAppMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAppMousePressed
        //Select active row
        Point point = evt.getPoint();
        int currentRow = tblApp.rowAtPoint(point);
        tblApp.setRowSelectionInterval(currentRow, currentRow);
        
        tblAppMouseClicked(null);
    }//GEN-LAST:event_tblAppMousePressed

    private void btnGetAppActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGetAppActionPerformed
        getApp(txtCustID_App.getText());
        btnSelectAll.setEnabled(true);
        mniSelectAll.setEnabled(true);
        btnDeselect.setEnabled(false);
        mniDeselect.setEnabled(false);
        btnPreBill.setEnabled(false);
        btnDelete.setEnabled(false);
        mniPreBill.setEnabled(false);
        mniDelete.setEnabled(false);
    }//GEN-LAST:event_btnGetAppActionPerformed

    private void btnSelectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectAllActionPerformed
        btnSelectAll.setEnabled(false);
        mniSelectAll.setEnabled(false);
        btnDeselect.setEnabled(true);
        mniDeselect.setEnabled(true);
        btnPreBill.setEnabled(true);
        btnDelete.setEnabled(true);
        mniPreBill.setEnabled(true);
        mniDelete.setEnabled(true);
        for (int i = 0; i < appModel.getRowCount(); i++)
            appModel.setValueAt(true, i, 1);
    }//GEN-LAST:event_btnSelectAllActionPerformed

    private void mniSelectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniSelectAllActionPerformed
        btnSelectAllActionPerformed(null);
    }//GEN-LAST:event_mniSelectAllActionPerformed

    private void btnDeselectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeselectActionPerformed
        btnSelectAll.setEnabled(true);
        mniSelectAll.setEnabled(true);
        btnDeselect.setEnabled(false);
        mniDeselect.setEnabled(false);
        btnPreBill.setEnabled(false);
        btnDelete.setEnabled(false);
        mniPreBill.setEnabled(false);
        mniDelete.setEnabled(false);
        for (int i = 0; i < appModel.getRowCount(); i++)
            appModel.setValueAt(false, i, 1);
    }//GEN-LAST:event_btnDeselectActionPerformed

    private void mniDeselectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniDeselectActionPerformed
        btnDeselectActionPerformed(null);
    }//GEN-LAST:event_mniDeselectActionPerformed

    private void btnPreBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreBillActionPerformed
        if ((JOptionPane.showConfirmDialog(this, "Are you sure want to Pre-bill selected Appointments ?", "Pre-bill comfirm", JOptionPane.OK_CANCEL_OPTION))== JOptionPane.OK_OPTION){
            getAuthority();
            int count = 0;
            for (int row = 0; row < appModel.getRowCount(); row++){
                if (hasSelected(row)) {
                    preBill(row);
                    count++;
                }
            }
        
            if (count == 0)
                JOptionPane.showMessageDialog(this, "Please select some records.");
        }
    }//GEN-LAST:event_btnPreBillActionPerformed

    private void mniPreBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniPreBillActionPerformed
        btnPreBillActionPerformed(null);
    }//GEN-LAST:event_mniPreBillActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        if ((JOptionPane.showConfirmDialog(this, "Are you sure want to delete selected Appointments ?", "Pre-bill comfirm", JOptionPane.OK_CANCEL_OPTION))== JOptionPane.OK_OPTION){
            int count = 0;
            for (int row = (tblApp.getRowCount()-1); row >= 0; row--){
                if (hasSelected(row)) {
                    deleteApp(row);
                    count++;
                }
            }
        
            if (count == 0)
                JOptionPane.showMessageDialog(this, "Please select some records.");
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void mniDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniDeleteActionPerformed
        btnDeleteActionPerformed(null);
    }//GEN-LAST:event_mniDeleteActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnExitActionPerformed

    private void tabCustStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabCustStateChanged
        if (tabCust.getSelectedIndex() == 1)
            txtCustID_App.grabFocus();
    }//GEN-LAST:event_tabCustStateChanged

    private void setEnabledForm (boolean enabled, boolean exceptID){        
        if (!exceptID)
            txtCustID.setEnabled(enabled);
        
        txtCustName.setEnabled(enabled);
        txtAdd.setEnabled(enabled);
        calDOB.setEnabled(enabled);
        radMale.setEnabled(enabled);
        radFemale.setEnabled(enabled);
        txtEmail.setEnabled(enabled);
        btnAddPic.setEnabled(enabled);
        btnSave.setEnabled(enabled);
        btnCancel.setEnabled(enabled);
    }
    
    private void setBackColorForm(Color color, boolean exceptID) {
        if (!exceptID)
            txtCustID.setBackground(color);
        
        txtCustName.setBackground(color);
        txtAdd.setBackground(color);
        txtEmail.setBackground(color);
    }
    
    private void clearForm(){
        txtCustID.setText("");
        txtCustName.setText("");
        calDOB.setDate(new Date());
        txtAdd.setText("");
        radMale.setSelected(true);
        txtEmail.setText("");
    }
        
    private boolean isValidated() {
        if (txtCustName.getText().isEmpty()){
            errorState("txtCustName","Customer's name is required. Try again.");
            return false;
        }
        return true;
    }
    
    private void errorState(String varName, String msg) {
        
        lblCustMsg.setText(msg);
        lblCustMsg.setForeground(new Color(255,0,0));
        
        try {
            Field var = this.getClass().getDeclaredField(varName);
            JTextField myTxt = (JTextField)var.get(this);
            myTxt.grabFocus();
            myTxt.setSelectionStart(0);
            myTxt.setSelectionEnd(myTxt.getText().length());
            myTxt.setBackground(new Color(255,204,204));
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
            Logger.getLogger(FrmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void validateState(String varName) {
        
        lblCustMsg.setForeground(new Color(240,240,240));
        
        try {
            Field var = this.getClass().getDeclaredField(varName);
            JTextField myTxt = (JTextField)var.get(this);
            myTxt.setBackground(new Color(255,255,255));
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
            Logger.getLogger(FrmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void resetEnvir() {
        btnInsert.setEnabled(true);
        btnUpdate.setEnabled(true);
        btnInsert.setForeground(new Color(0,51,51));
        btnUpdate.setForeground(new Color(0,51,51));
        tblCustEventOn = true;
        lostFocusEventOn = true;
        custPicName = null;
        custImg = null;
        lblPic.repaintBackground(custImg);
    }
    
    private boolean autocompletedPattern() {
        
        boolean mat_Short = Pattern.matches("\\d{1,6}", txtCustID.getText());
        boolean mat_Long = Pattern.matches("[C][u][s][t]\\d{6}", txtCustID.getText());
        int length = txtCustID.getText().length();
        if (mat_Short) {
            txtCustID.setText("Cust"+"000000".substring(0, 6-length)+txtCustID.getText());
            validateState("txtCustID");
            btnSave.setEnabled(true);
            return true;
        } else if (mat_Long){
            validateState("txtCustID");
            btnSave.setEnabled(true);
            return true;
        } else if (txtCustID.getText().isEmpty()) {
            errorState("txtCustID","Customer's ID is required. Try again.");
        }  else {
            errorState("txtCustID","Invalid Customer's ID (Cust000001 e.g). Try again.");
        }
        
        btnSave.setEnabled(false);
        return false;
    }
    
    private void bindingData(String custID) {
        
        try {
            String sql = "SELECT * FROM Customer WHERE CustID = ?";
            param.clear();
            param.add(custID);
            dc.rs = dc.prepareSelect(dc.preSt_binCust,sql, param);
            
            dc.rs.last();
            if (dc.rs.getRow()==1) {
                txtCustID.setText(dc.rs.getString("CustID"));
                txtCustName.setText(dc.rs.getString("CustName"));
                txtAdd.setText(dc.rs.getString("Addr"));
                calDOB.setDate(next2Days(new Date(dc.rs.getDate("DOB").getTime())));
                calDOB.updateUI();
                gender = dc.rs.getBoolean("Gender");
                if (gender)
                    radFemale.setSelected(true);
                else
                    radMale.setSelected(true);
                txtEmail.setText(dc.rs.getString("Email"));
                custPicName = dc.rs.getString("Picture");
                if (custPicName == null) 
                    custImg = new ImageIcon(getClass().getResource("/images/NoImage.jpg")).getImage();
                else {
                     try {
                        custImg = ImageIO.read(new File("images/Cust_Img/" + custPicName + ".jpg"));
                    } catch (Exception e) {
                        custImg = new ImageIcon(getClass().getResource("/images/NoImage.jpg")).getImage();
                    }
                }
                lblPic.repaintBackground(custImg);
            } else {
                errorState("txtCustID","'" + txtCustID.getText()+"'"+" could not be found. Try again.");
            }
        } catch (SQLException ex) {
            Logger.getLogger(FrmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void saveData() {
        if(type  == mode.Insert){
            try {  
                String sql = "INSERT INTO Customer VALUES(?,?,?,?,?,?,?,?,?,?,?)";
                param.clear();
                param.add(txtCustID.getText());
                param.add(txtCustName.getText());
                param.add(radMale.isSelected()?false:true);
                param.add(txtAdd.getText().isEmpty()?null:txtAdd.getText());
                param.add(txtEmail.getText().isEmpty()?null:txtEmail.getText());
                param.add(new java.sql.Date(calDOB.getDate().getTime()));
                param.add(custPicName);
                param.add(new java.sql.Date(new Date().getTime()));
                param.add(User);
                param.add(null);
                param.add(null);
                
                dc.prepareUpdate(dc.preSt_insertCust, sql, param);
                
                if (custImg != null)
                    ImageIO.write((BufferedImage) custImg, "jpg", new File ("images/Cust_Img/"+custPicName+".jpg"));
                lblCustMsg.setForeground(new Color(255,0,0));
                lblCustMsg.setText("Insert successfully.");
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Can't save Customer's image. Try again.");
            }
        } else if (type == mode.Update) {
            String sql = "UPDATE Customer SET CustName = ?, "       +
                                             "Gender = ?, "         +
                                             "Addr = ?, "           +
                                             "Email = ?, "          +
                                             "DOB = ?, "            +
                                             "Picture = ?, "        +
                                             "DateModified = ?, "   +
                                             "UserModified = ? "    +
                                             "WHERE CustID = ?"     ;
            
            param.clear();
            param.add(txtCustName.getText());
            param.add(radMale.isSelected()?false:true);
            param.add(txtAdd.getText().isEmpty()?null:txtAdd.getText());
            param.add(txtEmail.getText().isEmpty()?null:txtEmail.getText());
            param.add(new java.sql.Date(calDOB.getDate().getTime()));
            param.add(custPicName);
            param.add(new java.sql.Date(new Date().getTime()));
            param.add(User);
            param.add(txtCustID.getText());
            
            dc.prepareUpdate(dc.preSt_updateCust, sql, param);
            try {
                if (custImg != null && !(custImg instanceof ToolkitImage))
                    ImageIO.write((BufferedImage) custImg, "jpg", new File ("images/Cust_Img/"+custPicName+".jpg"));
                lblCustMsg.setForeground(new Color(255,0,0));
                lblCustMsg.setText("'"+txtCustID.getText()+"' has been updated.");
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Can't save Customer's image. Try again.");
            }
        }
    }
    
    private void updateTable() {
        
        if (type == mode.Insert) {
            transpoter = new Vector();
            transpoter.add(txtCustID.getText());
            transpoter.add(txtCustName.getText());
            transpoter.add(gender);
            transpoter.add(txtAdd.getText());
            transpoter.add(dateFormat.format(calDOB.getDate()));
            custModel.addRow(transpoter);
            //lblCustFound.setText(String.valueOf(Integer.parseInt(lblCustFound.getText().substring(0, 1))+1)+" records found.");                        
        } else if (type == mode.Update) {
            int noRow = custModel.getRowCount();
            for (int i = 0; i < noRow; i++) {
                if (custModel.getValueAt(i, 0).equals(txtCustID.getText())) {
                    custModel.setValueAt(txtCustName.getText(), i, 1);
                    custModel.setValueAt(gender, i, 2);
                    custModel.setValueAt(txtAdd.getText(), i, 3);
                    custModel.setValueAt(dateFormat.format(calDOB.getDate().getTime()), i, 4);
                    break;
                }
            }
        }
    }
    private void addPicture() {
        
        JFileChooser file = new JFileChooser("H:\\Hinh\\Sister");
        FileNameExtensionFilter  fileExt =  new FileNameExtensionFilter("Images (*.jpg, *.jpeg, *.png", new String[] {"jpg", "jpeg", "png"});
        file.setFileFilter(fileExt);
        
        int action = file.showOpenDialog(this);
        if (action == JFileChooser.APPROVE_OPTION){
            try {
                custImg = ImageIO.read(file.getSelectedFile());
                custPicName = txtCustID.getText();
                lblPic.repaintBackground(custImg);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Image is damaged. Please choose another one.");
            }
        }
    }
    
    private String generateCustID() {
        String CustID, SufID, MaskID = "Cust000001";
        int IDNum;
        try {
            String sql = "{call LargestCustID(?)}";
            dc.calSt = dc.con.prepareCall(sql);
            dc.calSt.registerOutParameter(1, java.sql.Types.CHAR);
            dc.calSt.execute();
            CustID = dc.calSt.getString(1);
            
            if (CustID == null) 
                return MaskID;
            
            IDNum = Integer.parseInt(CustID.substring(4)); IDNum++;
            SufID = Integer.toString(IDNum);
            return MaskID.substring(0,10-SufID.length())+SufID;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Connection error. Try again later.");
            return null;
        }
    }
    
    private String generateAppID() {
        String AppID, SufID;
        String MaskAppID = new SimpleDateFormat("yyyyMMdd").format(new Date())+"-App001";
        int IDNum;
        try {
            String sql = "SELECT TOP 1 AppID FROM AppMaster  WHERE AppID LIKE ? ORDER BY AppID DESC";
            param.clear();
            param.add("%"+MaskAppID.substring(0, 8)+"%");
            
            dc.rs = dc.prepareSelect(dc.preSt_genAppID, sql, param);
            
            if (dc.rs.next()) {
                AppID = dc.rs.getString("AppID");
                IDNum = Integer.parseInt(AppID.substring(12)); IDNum++;
                SufID = Integer.toString(IDNum);
                return MaskAppID.substring(0,15-SufID.length())+SufID;
            }
            return MaskAppID;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Connection error. Try again later.");
            return null;
        }
    }
    
    private void selectText(String varName) {
        try {
            Field var  = this.getClass().getDeclaredField(varName);
            JTextField txt = (JTextField)var.get(this);
            txt.grabFocus();
            txt.setSelectionStart(0);
            txt.setSelectionEnd(txt.getText().length());
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
            Logger.getLogger(FrmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private Date next2Days(Date curDate) {
        
        cal.setTime(curDate);
        cal.add(Calendar.DATE, 2);
        return cal.getTime();
    }
    
    private void newApp(String custID) {
        getAuthority();
        String sql = "SELECT * FROM vw_cust_appmaster WHERE AppDate = ? AND CustID = ? AND Status IN (?,?)";
        param.clear();
        param.add(new java.sql.Date(new Date().getTime()));
        param.add(custID);
        param.add("Processing");
        param.add("Late-bill");
        
        dc.rs = dc.prepareSelect(dc.preSt_HasApp, sql, param);
        try {
            if (dc.rs.next()) {
                JOptionPane.showMessageDialog(this, "'"+custID+"' already had an appointment or didn't make payment yet.");
                return;
            }
        } catch (SQLException ex) {
            Logger.getLogger(FrmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
        sql = "INSERT INTO AppMaster(AppID, CustID, UserCreated) VALUES (?,?,?)";
        String appID = generateAppID();
        param.clear();
        param.add(appID);
        param.add(custID);
        param.add(User);
        
        dc.prepareUpdate(dc.preSt_newApp, sql, param);
        JOptionPane.showMessageDialog(this, "Appointment had been made.");
    }
    
    private int searchCust(boolean findAll, String cond) {
        
        String sqlcmd;
        int noRecord = 0;
        
        custModel.setRowCount(0);
        if (findAll) {
            sqlcmd = "SELECT * FROM Customer ORDER BY CustName";
            dc.rs = dc.selectCommandNoPara(sqlcmd);
        } else {
            if (way == searchBy.byID)
                sqlcmd = "SELECT * FROM Customer WHERE CustID = ?";
            else  
                sqlcmd = "SELECT * FROM Customer WHERE CustName LIKE ? ORDER BY CustName";
            
            param.clear();
            param.add(cond);
            
            dc.rs = dc.prepareSelect(dc.preSt_searchCust, sqlcmd, param);
        }
        
        try {
           while (dc.rs.next()){
               transpoter = new Vector();
               transpoter.add(dc.rs.getString("CustID"));
               transpoter.add(dc.rs.getString("CustName"));
               transpoter.add(dc.rs.getBoolean("Gender"));
               transpoter.add(dc.rs.getString("Addr"));
               transpoter.add(dateFormat.format(next2Days(dc.rs.getDate("DOB"))));
               custModel.addRow(transpoter);
               noRecord++;
           }
        } catch (SQLException ex) {
            Logger.getLogger(FrmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        if (noRecord == 0) {
            lblCustFound.setText("No record found.");
        } else {
            lblCustFound.setText(noRecord + " records found.");
        }
        lblCustFound.setForeground(Color.red);
        return noRecord;
    }
    
    private int getApp(String custID) {
        
        int noRecord = 0;
        
        appModel.setRowCount(0);
        String sql = "SELECT * FROM vw_cust_appmaster WHERE CustID = ?";
        param.clear();
        param.add(custID);
        
        dc.rs = dc.prepareSelect(dc.preSt_getApp, sql, param);
        
        try {
           while (dc.rs.next()){
               transpoter = new Vector();
               transpoter.add(dc.rs.getString("AppID"));
               transpoter.add(false);
               transpoter.add(dateFormat.format(next2Days(dc.rs.getDate("AppDate"))));
               transpoter.add(dc.rs.getString("CustName"));
               transpoter.add(dc.rs.getString("Status"));
               appModel.addRow(transpoter);
               noRecord++;
           }
        } catch (SQLException ex) {
            Logger.getLogger(FrmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        if (noRecord == 0) {
            lblAppFound.setText("No record found.");
        } else {
            lblAppFound.setText(noRecord + " records found.");
        }
        lblAppFound.setForeground(Color.red);
        return noRecord;
    }
    
    private boolean hasSelected(int row) {
       row = tblApp.convertRowIndexToModel(row);
       if ((boolean)appModel.getValueAt(row, 1) == true)
           return true;
       return false;
    }
    
    private void preBill(int tableRow) {
       
        int row = tblApp.convertRowIndexToModel(tableRow);
        String sql, status, AppID;
        String errMsg = "Appointment has not finished yet.";
        status = (String)appModel.getValueAt(row, 4);
        AppID = (String)appModel.getValueAt(row, 0);
        
        if (status.equalsIgnoreCase("Complete") || status.equalsIgnoreCase("Late-bill")) {
            sql = "UPDATE AppMaster SET Status = ?, DateModified = ?, UserModified = ? WHERE AppID = ?";
            param.clear();
            param.add("Pre-bill");
            param.add(new java.sql.Date(new Date().getTime()));
            param.add(User);
            param.add(AppID);
            dc.prepareUpdate(dc.preSt_preBill, sql, param);
            appModel.setValueAt("Pre-bill", row, 4);
        } else {
            appModel.setValueAt(errMsg, row, 5);
        }
        
        appModel.setValueAt(false, row, 1);
        for (int col = 1 ; col <= 5 ; col++){
            appModel.fireTableCellUpdated(row, col);
        }
    }
    
    private void deleteApp(int tableRow) {
        
        int row = tblApp.convertRowIndexToModel(tableRow);
        String sql, status, AppID;
        String errMsg = "Appointment has not finished yet.";
        status = (String)appModel.getValueAt(row, 4);
        AppID  = (String)appModel.getValueAt(row, 0);
        
        param.clear();
        param.add(AppID);
        
        if (status.equalsIgnoreCase("Processing")) {
            sql = "SELECT AppDetails.Status FROM AppMaster INNER JOIN AppDetails ON AppMaster.AppID = AppDetails.AppID WHERE AppMaster.AppID = ?";    
            dc.rs = dc.prepareSelect(dc.preSt_checkAppToDelete, sql, param);
            
            try {
                while (dc.rs.next()) {
                    status = dc.rs.getString("Status");
                    if (status.equalsIgnoreCase("Canceled") || status.equalsIgnoreCase("Finished")) {
                        updateModel(appModel,errMsg,row);
                        return;
                    }
                }
                
                sql = "DELETE FROM AppMaster WHERE AppID = ?";
                dc.prepareUpdate(dc.preSt_delApp, sql, param);
                appModel.removeRow(row);
            } catch (SQLException ex) {
                Logger.getLogger(FrmCustomer.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            updateModel(appModel,errMsg,row);
        }
    }
    
    private void updateModel(DefaultTableModel tblModel, String errMsg, int row) {
        tblModel.setValueAt(errMsg, row, 5);
        tblModel.setValueAt(false, row, 1);
        for (int col = 1 ; col <= 5 ; col++)
            tblModel.fireTableCellUpdated(row, col);
    }
    
    private void displayAppForm() {
        
        String custName = (String)tblCust.getValueAt(tblCust.getSelectedRow(), 1);
        try {
            Field var = getDesktopPane().getTopLevelAncestor().getClass().getDeclaredField("fApp");
            JInternalFrame appForm = (JInternalFrame)var.get(getDesktopPane().getTopLevelAncestor());
            if (appForm == null || appForm.isClosed()) {
                appForm = (JInternalFrame)FrmAppointment.class.getConstructor(DBConnection.class, String.class, boolean.class).newInstance(dc, custName, false);
                var.set(getDesktopPane().getTopLevelAncestor(), appForm);
                appForm.setVisible(true);
                getDesktopPane().add(appForm);
            }
            appForm.setSelected(true);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | PropertyVetoException | NoSuchMethodException | InstantiationException | InvocationTargetException ex) {
            Logger.getLogger(FrmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddPic;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnDeselect;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnFindAll;
    private javax.swing.JButton btnGetApp;
    private javax.swing.JButton btnInsert;
    private javax.swing.JButton btnPreBill;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnSelectAll;
    private javax.swing.JButton btnUpdate;
    private org.freixas.jcalendar.JCalendarCombo calDOB;
    private javax.swing.ButtonGroup grpFilter;
    private javax.swing.ButtonGroup grpGender;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JScrollPane jspCust;
    private javax.swing.JScrollPane jspCust1;
    private javax.swing.JLabel lblAdd;
    private javax.swing.JLabel lblAppFound;
    private javax.swing.JLabel lblCustFound;
    private javax.swing.JLabel lblCustID;
    private javax.swing.JLabel lblCustID_App;
    private javax.swing.JLabel lblCustMsg;
    private javax.swing.JLabel lblCustName;
    private javax.swing.JLabel lblDOB;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblGender;
    private PhotoFrame lblPic;
    private javax.swing.JLabel lblRequired1;
    private javax.swing.JLabel lblRequired2;
    private javax.swing.JMenuItem mniDelete;
    private javax.swing.JMenuItem mniDeselect;
    private javax.swing.JMenuItem mniNewApp;
    private javax.swing.JMenuItem mniNewAppFull;
    private javax.swing.JMenuItem mniPreBill;
    private javax.swing.JMenuItem mniSelectAll;
    private javax.swing.JMenuItem mniViewApp;
    private javax.swing.JPopupMenu pmnApp;
    private javax.swing.JPopupMenu pmnCust;
    private javax.swing.JPanel pnlApp;
    private javax.swing.JPanel pnlAppChild;
    private javax.swing.JPanel pnlButton;
    private javax.swing.JPanel pnlChildCust;
    private javax.swing.JPanel pnlCust;
    private javax.swing.JPanel pnlSearch;
    private javax.swing.JPanel pnlSearchChild;
    private javax.swing.JRadioButton radCustID;
    private javax.swing.JRadioButton radCustName;
    private javax.swing.JRadioButton radFemale;
    private javax.swing.JRadioButton radMale;
    private javax.swing.JTabbedPane tabCust;
    private javax.swing.JTable tblApp;
    private javax.swing.JTable tblCust;
    private javax.swing.JTextField txtAdd;
    private javax.swing.JTextField txtCustID;
    private javax.swing.JTextField txtCustIDFilter;
    private javax.swing.JTextField txtCustID_App;
    private javax.swing.JTextField txtCustName;
    private javax.swing.JTextField txtCustNameFilter;
    private javax.swing.JTextField txtEmail;
    // End of variables declaration//GEN-END:variables
}

class PhotoFrame extends javax.swing.JLabel {
    
    Image image;
    
    public PhotoFrame(){super();};

    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
    }
    
    public void repaintBackground(Image newImage) {
        image = newImage;
        repaint();
    }
}

class tblAppRender implements TableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            
            row = table.convertRowIndexToModel(row);
            
            if (column != 1) {
                JTextField editor = new JTextField();
                
                editor.setBorder(null);
                if (value != null)
                    editor.setText(value.toString());
                
                if (isSelected) {
                    if (table.getModel().getValueAt(row, 5) == null) {
                        editor.setBackground(table.getSelectionBackground());
                        editor.setForeground(table.getSelectionForeground());
                    //select.setBackground(table.getSelectionBackground());
                    } else {
                        editor.setBackground(new Color(231, 92, 137));
                        editor.setForeground(new Color(255, 255, 255));
                    //select.setBackground(new Color(231, 92, 137));
                    }
                } else {
                    if (table.getModel().getValueAt(row, 5) == null) {
                        editor.setBackground(new Color(255, 255, 255));
                        editor.setForeground(new Color(0, 102, 51));
                    //select.setBackground(new Color(255, 255, 255));
                    } else {
                        editor.setBackground(new Color(255, 204, 204));
                        editor.setForeground(new Color(153, 0, 0));
                        //select.setBackground(new Color(255, 204, 204));
                    }
                }
                return editor;
            } else {     
                JCheckBox select = new JCheckBox();
                select.setSelected((boolean)value);
                select.setHorizontalAlignment((int)CENTER_ALIGNMENT);
                
                if (isSelected) {
                    if (table.getModel().getValueAt(row, 5) == null) {
                        select.setBackground(table.getSelectionBackground());
                    } else {
                        select.setBackground(new Color(231, 92, 137));
                    }
                } else {
                    if (table.getModel().getValueAt(row, 5) == null) {
                        select.setBackground(new Color(255, 255, 255));
                    } else {
                        select.setBackground(new Color(255, 204, 204));
                    }
                }
                return select;
            }
        }
    }